package com.hc.config;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class TransactionResultHolder {
    public static Map<Class<?>, Map<String, String>> map = new ConcurrentHashMap<>();

    /**
     * 设置状态
     */
    public static void setResult(Class<?> clazz, String xid, String value) {
        Map<String, String> classMap = map.get(clazz);
        if (classMap == null) {
            synchronized (map) {
                if (classMap == null) {
                    classMap = new ConcurrentHashMap<>();
                    map.put(clazz, classMap);
                }
            }
        }
        classMap.put(xid, value);
    }

    /**
     * 获取状态
     */
    public static String getResult(Class<?> clazz, String xid) {
        Map<String, String> classMap = map.get(clazz);
        if (classMap != null) {
            return classMap.get(xid);
        }
        return null;
    }

    /**
     * 释放状态
     */
    public static void releaseResult(Class<?> clazz, String xid) {
        Map<String, String> classMap = map.get(clazz);
        if (classMap != null) {
            classMap.remove(xid);
        }
    }

}